﻿using System;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BCDataCenter;
using BCDataCenter.BOL;

public partial class WebMaster_Administrator_Membership_Default : System.Web.UI.Page
{
    static string error = "";
    string str = BCDataCenter.Configuration.CurrentConnectionString;
    ClassExecute.ClassExecute dt;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    static public string GetMd5Sum(string str)
    {
        // First we need to convert the string into bytes, which
        // means using a text encoder.

        Encoder enc = System.Text.Encoding.Unicode.GetEncoder();
        // Create a buffer large enough to hold the string

        byte[] unicodeText = new byte[str.Length * 2];

        enc.GetBytes(str.ToCharArray(), 0, str.Length, unicodeText, 0, true);

        // Now that we have a byte array we can ask the CSP to hash it

        MD5 md5 = new MD5CryptoServiceProvider();

        byte[] result = md5.ComputeHash(unicodeText);

        // Build the final string by converting each byte

        // into hex and appending it to a StringBuilder

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < result.Length; i++)
        {

            sb.Append(result[i].ToString("X2"));

        }
        // And return it
        return sb.ToString();
    }
    protected void ChangePasswordPushButton_Click(object sender, EventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        int i = dt.ExecuteSQl_int(str, "K_UpdateChangePass", CommandType.StoredProcedure, ref error,
            new SqlParameter("@NewPassHint", NewPassword.Text),
            new SqlParameter("@NewPassword",GetMd5Sum(NewPassword.Text)),
            new SqlParameter("@OldPassword", CurrentPassword.Text),
            new SqlParameter("@UserName", Session["username"].ToString()));
        if (i == 1)
        {
            uscMsgBox1.AddMessage("Mật khẩu cũ không đúng", uscMsgBox.enmMessageType.Error);
        }
        else if (i == 0)
        {
            uscMsgBox1.AddMessage("Bạn đã cập nhật mật khẩu thành công", uscMsgBox.enmMessageType.Success);

        }
    }
    
}
